<template>

 <el-row :gutter="20" style="padding-left: 20px;padding-right: 20px;padding-top: 10px;">
    <el-card class="box-card" style="width: 100%;">
      <template #header>
        <div class="card-header">
          <span>我发起的项目</span>
          <el-button class="button" text>
            <el-icon style="cursor: pointer;">
        <ArrowRight />
      </el-icon>
          </el-button>
        </div>
      </template>
      <div class="text item">
        <el-table :data="myList" border >
      <el-table-column label="项目编号" align="center" width="180" prop="projectNo" />
      <el-table-column label="项目名称" align="center" width="200" prop="projectName" />
      <el-table-column label="发起人" align="center" prop="createUserName" />
      <el-table-column label="合同金额" align="center" show-overflow-tooltip width="200" prop="contractAmount" />
      <el-table-column label="结算金额" align="center" show-overflow-tooltip width="200" prop="settlementAmount" />
      <el-table-column label="项目类型" align="center" width="120" prop="projectType">
        <template #default="scope">
          <dict-tag :options="project_type" :value="scope.row.projectType" />
        </template>
      </el-table-column>
      <el-table-column label="立项日期" align="center" prop="approvalDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.approvalDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计开始日期" align="center" prop="planDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计验收日期" align="center" prop="acceptTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.acceptTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="管家婆项目" align="center" prop="housekeeperName" show-overflow-tooltip width="300" />
      <el-table-column label="操作" fixed="right" width="220" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
      </div>
    </el-card>
</el-row>


<el-row :gutter="20" style="padding-left: 20px;padding-right: 20px;padding-top: 10px;">
    <el-card class="box-card" style="width: 100%;">
      <template #header>
        <div class="card-header">
          <span>需我处理的项目</span>
          <el-button class="button" text>
            <el-icon style="cursor: pointer;">
        <ArrowRight />
      </el-icon>
          </el-button>
        </div>
      </template>
      <div class="text item">
        <el-table  :data="needList" border>
      <el-table-column label="项目编号" align="center" width="180" prop="projectNo" />
      <el-table-column label="项目名称" align="center" width="200" prop="projectName" />
      <el-table-column label="发起人" align="center" prop="createUserName" />
      <el-table-column label="合同金额" align="center" show-overflow-tooltip width="200" prop="contractAmount" />
      <el-table-column label="结算金额" align="center" show-overflow-tooltip width="200" prop="settlementAmount" />
      <el-table-column label="项目类型" align="center" width="120" prop="projectType">
        <template #default="scope">
          <dict-tag :options="project_type" :value="scope.row.projectType" />
        </template>
      </el-table-column>
      <el-table-column label="立项日期" align="center" prop="approvalDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.approvalDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计开始日期" align="center" prop="planDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计验收日期" align="center" prop="acceptTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.acceptTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="管家婆项目" align="center" prop="housekeeperName" show-overflow-tooltip width="300" />
      <el-table-column label="操作" fixed="right" width="220" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
      </div>
    </el-card>
</el-row>



<el-row :gutter="20" style="padding-left: 20px;padding-right: 20px;padding-top: 10px;">
    <el-card class="box-card" style="width: 100%;">
      <template #header>
        <div class="card-header">
          <span>我参与的项目</span>
          <el-button class="button" text>
            <el-icon style="cursor: pointer;" >
        <ArrowRight />
      </el-icon>
          </el-button>
        </div>
      </template>
      <div  class="text item">
        <el-table  :data="joinList" border >
      <el-table-column label="项目编号" align="center" width="180" prop="projectNo" />
      <el-table-column label="项目名称" align="center" width="200" prop="projectName" />
      <el-table-column label="发起人" align="center" prop="createUserName" />
      <el-table-column label="合同金额" align="center" show-overflow-tooltip width="200" prop="contractAmount" />
      <el-table-column label="结算金额" align="center" show-overflow-tooltip width="200" prop="settlementAmount" />
      <el-table-column label="项目类型" align="center" width="120" prop="projectType">
        <template #default="scope">
          <dict-tag :options="project_type" :value="scope.row.projectType" />
        </template>
      </el-table-column>
      <el-table-column label="立项日期" align="center" prop="approvalDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.approvalDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计开始日期" align="center" prop="planDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计验收日期" align="center" prop="acceptTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.acceptTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="管家婆项目" align="center" prop="housekeeperName" show-overflow-tooltip width="300" />
      <el-table-column label="操作" fixed="right" width="220" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
      </div>
    </el-card>

</el-row> 
   <el-dialog v-model="openView" fullscreen append-to-body>
      <projectView :datas="form" :isView="isView" :column="3" :sizes="'large'" :personList="personList"
        :attachmentList="attachmentList" :payList="payList"  :warrantyList="warrantyList" :implementList="implementList" :accountingList="accountingList"></projectView>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

</template>

<script setup name="homeIndex">
import {
  ArrowRight,
} from '@element-plus/icons-vue'
import {
  getProject,listPhase,listAccounting,
  listParticipan, getPermission,listImplement,getResults,myProject,listAttachment,listPay,listWarranty
} from "@/api/project/project";

import projectView from "../project/projectView.vue";

const { proxy } = getCurrentInstance();
const myList = ref([]);
const needList = ref([]);
const joinList = ref([]);


const { project_type } = proxy.useDict("project_type");
const attachmentList = ref([]);
const payList = ref([]);
const personList = ref([]);
const accountingList = ref([]);
const implementList = ref([]);
const logs = ref([]);
const warrantyList = ref([]);
const openView = ref(false);
const isView = ref(false);
const ids = ref([]);
const projectNo = ref("")
const finance = ref(false);//当期人员是否是财务
const isAdmin = ref(false);//是否是超级管理员
const havePay = ref(false);//是否有添加付款信息的权限
const status = ref("0");
const formDisabled = ref(true);

// 表单重置
function reset() {
  form.value = {
  };
  proxy.resetForm("projectRef");
}

const data = reactive({
  currentProject: {},
  form: {
    createUserName: null,
    createUserId: null,
    createDeptName: null,
    createDept: null,
  },
  personForm: {
  },
  fileForm: {

  },
  warrantyForm: {

  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 2,//写死 查询上个阶段已完成的项目 1是进行中 2是已完成 
    // 新建项目的时候会默认插入一条立项阶段到立项表 点击下一步的时候会更新阶段表把当前项目当前阶段的状态
    //改为已完成  同时往阶段表插入下一个阶段 状态是1 进行中
    phaseId: 5,//阶段字典id 查询已归档
    projectName: null,
    projectAmount: null,
    projectType: null,
    projectUnit: null,
    acceptTime: null,
    customerName: null,
    housekeeperName: null,
    phaseCode: null,
    minProjectAmount: null,
    maxProjectAmount: null
  },
  rules: {
    projectName: [{ required: true, message: "项目名称不能为空", trigger: "blur" }],
    approvalDate: [{ required: true, message: "立项日期不能为空", trigger: "blur" }],
    projectType: [{ required: true, message: "项目类型 不能为空", trigger: "blur" }],
  },

  warrantyRules: {
    company: [{ required: true, message: "付款单位不能为空", trigger: "blur" }],
    settlementAmount: [{ required: true, message: "结算金额不能为空", trigger: "blur" }],
    settlementAmount: [{ required: true, message: "结算金额不能为空,请联系负责人在项目信息中填写结算金额", trigger: "blur" }],
  },

  result:{

  }

});

const { queryParams, form, personForm, payRules, warrantyRules, fileForm, warrantyForm,result } = toRefs(data);


/** 阶段管理操作 */
async function handleView(row) {
  formDisabled.value = true
  isView.value = true
  reset();
  getInfo(row.projectNo)
  const _projectNo = row.projectNo || ids.value;
  await getProject(_projectNo).then((response) => {
    form.value = response.data;
    openView.value = true;
  });

}
// 取消按钮
function cancel() {
  openView.value = false
//   reset();
}

//是否是财务
async function isFinance() {
  let params = {
    projectNo: projectNo.value,
    // phaseId: 1,//立项前期 写死
    postIds: [2, 6]//传入岗位id    需要添加项目经理id 2  销售负责人 5  销售 9 商务 7 财务 6
  }
  finance.value = await hasPermisson(params)

  console.log("添加财务信息", finance.value);
}

async function hasPermisson(params) {
  const res = await getPermission(params).then((response) => {
    console.log("权限信息", response.data);
    return response.data
  });
  return res
}


async function isSale() {
  let params = {
    projectNo: projectNo.value,
    // phaseId: 1,//立项前期 写死
    postIds: [2, 5, 6, 7]//传入岗位id    需要添加项目经理id 2  销售负责人 5  销售 9 商务 7 财务 6
  }
  havePay.value = await hasPermisson(params)
  console.log("添加付款信息", havePay.value);

}


function getInfo(projectNo) {
  console.log("projectNoprojectNo", projectNo);

  let params = {
    projectNo: projectNo
  }
  
  getResults(projectNo).then((response) => {
    console.log("xxxxxxx",response);
    result.value = {...response.data}
    console.log("统计信息",result.value);
  });

  //参与人
  listParticipan(params).then((response) => {
    console.log("参与人员", response);
    personList.value = response.data
  });

  //附件
  listAttachment(params).then((response) => {
    attachmentList.value = response.data;
  });

  //付款信息
  listPay(params).then((response) => {
    payList.value = response.data;
  });

  //核算信息
  listAccounting(params).then((response) => {
    accountingList.value = response.data;
  });

  //质保信息
  listWarranty(params).then((response) => {
    warrantyList.value = response.data;
  });

  listImplement(params).then((response) => {
    implementList.value = response.data;
  });


  listPhase(params).then((response) => {
    logs.value = response.data.list;

  });



//   isFinance()
//   isSale()

  let currPhase = {
    projectNo: projectNo,
    phaseId: 1
  }


}

function getInitData(){
    myProject(null).then((response) => {
    myList.value = response.data.myList
    needList.value = response.data.needList
    joinList.value = response.data.joinList

  });

}

getInitData()

</script>
<style scoped>
</style>